package com.zhouyibike.uu.ui.activity.mybike;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.CustomerDialog;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class ZhuanRangBackActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private int zrtotalnum;
    private int zrnum=1;
    private TextView tv_totalnum;
    private String bicyclePrice;
    private TextView tv_jian,tv_jia;
    private EditText et_numbershow;
    private TextView tv_zrnumber,tv_zrprice;
    private Button btn_zr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuan_rang_back);
        zrtotalnum=getIntent().getIntExtra("cxzr",0);
        bicyclePrice=preferences.getString("bicyclePrice","0");
        initView();
    }
    private void initView(){
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("撤销转让");
        mBtnBack.setOnClickListener(this);
        tv_totalnum= (TextView) findViewById(R.id.tv_zrbike_num);
        tv_totalnum.setText("请输入要撤销转让的单车数量(可撤销"+zrtotalnum+"辆)");
        tv_jian= (TextView) findViewById(R.id.tv_zr_jian);
        tv_jian.setOnClickListener(this);
        tv_jia= (TextView) findViewById(R.id.tv_zr_jia);
        tv_jia.setOnClickListener(this);
        et_numbershow= (EditText) findViewById(R.id.et_zr_numshow);
        tv_zrnumber= (TextView) findViewById(R.id.tv_zr_zrnumber);
        tv_zrprice= (TextView) findViewById(R.id.tv_zr_zrprice);
        tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice)*zrnum)+"元");
        btn_zr= (Button) findViewById(R.id.btn_mybike_buy);
        btn_zr.setOnClickListener(this);
        et_numbershow.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (et_numbershow.getText().toString().length()<1)
                {
                    return;
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
//                    try {
//                        if (Integer.parseInt(s.toString()) < 1 || Integer.parseInt(s.toString()) > zrtotalnum) {
//                            et_numbershow.setText(zrnum + "");
//                            toastShow("输入的数量不在范围之内");
//                        } else {
//                            zrnum = Integer.parseInt(et_numbershow.getText().toString());
//                            et_numbershow.setSelection(s.length());//将光标移至文字末尾
//                            tv_zrnumber.setText(zrnum + "辆");
//                            tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice) * zrnum) + "元");
//                        }
//                    } catch (Exception e) {
//                        zrnum = 0;
//                        tv_zrnumber.setText(zrnum + "辆");
//                        tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice) * zrnum) + "元");
//                    }
                if (et_numbershow.getText().toString().length()<1||Integer.parseInt(et_numbershow.getText().toString())<0)
                {
                    tv_jian.setEnabled(false);
                    zrnum = 0;
                    et_numbershow.setSelection(s.length());//将光标移至文字末尾
                    tv_zrnumber.setText(zrnum + "辆");
                    tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice) * zrnum) + "元");
                }else{
                    tv_jia.setEnabled(true);
                    tv_jian.setEnabled(true);
                    zrnum = Integer.parseInt(et_numbershow.getText().toString());
                    if (zrnum==0)
                    {
                        tv_jian.setEnabled(false);
                    }
                    et_numbershow.setSelection(s.length());//将光标移至文字末尾
                    tv_zrnumber.setText(zrnum + "辆");
                    tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice) * zrnum) + "元");
                }
            }
        });
    }
    DecimalFormat df = new DecimalFormat("######0.00");
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.tv_zr_jian:
                if(zrnum==0){
                  //  toastShow("不能再少了");
                    tv_jian.setEnabled(false);
                }else
                {
                    zrnum--;
                    et_numbershow.setText(zrnum+"");
                    et_numbershow.setSelection(et_numbershow.getText().toString().length());//将光标移至文字末尾
                    tv_zrnumber.setText(zrnum+"辆");
                    tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice)*zrnum)+"元");
                }
                break;
            case R.id.tv_zr_jia:
//                if(zrnum==zrtotalnum){
//                    tv_jia.setEnabled(false);
//                    toastShow("不能再多了");
//                }else
                {
                    zrnum++;
                    et_numbershow.setText(zrnum+"");
                    et_numbershow.setSelection(et_numbershow.getText().toString().length());//将光标移至文字末尾
                    tv_zrnumber.setText(zrnum+"辆");
                    tv_zrprice.setText(df.format(Double.parseDouble(bicyclePrice)*zrnum)+"元");
                }
                break;
            case R.id.btn_mybike_buy:
                zrdialog= CustomerDialog.createDeletaDialog(ZhuanRangBackActivity.this, "您确定要撤销转让吗？", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()){
                            case R.id.tv_deletedialog_cancle:
                                zrdialog.dismiss();
                                break;
                            case R.id.tv_deletedialog_delete:
                                zrdialog.dismiss();
                                if (zrnum>zrtotalnum)
                                {
                                    toastShow("可撤销单车数量不足");
                                    break;
                                }else if (zrnum==0){
                                    toastShow("请输入正确的撤销数量");
                                    break;
                                }
                                getMsg();
                                break;
                        }
                    }
                });
                zrdialog.show();

                break;
        }
    }
    private Dialog zrdialog;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==10010&&resultCode==1){
            finish();
        }
    }

    private void getMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("count",zrnum+"");
        showProgressDialog();
        addSubscription(apiStores.ApiBackOutBike(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            setResult(1);
//                            zrtotalnum=zrtotalnum-zrnum;
//                            tv_totalnum.setText("请输入要转让的单车数量(可转让单车"+zrtotalnum+"辆)");
//                            startActivityForResult(new Intent(ZhuanRangBackActivity.this,ZRSuccessActivity.class),10010);
                            toastShow("撤销成功");
                            finish();
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
